package gbench.sandbox.matrix;


import org.junit.jupiter.api.Test;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.stream.Stream;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;

import gbench.common.tree.LittleTree.IRecord;
import gbench.commonApp.data.DataMatrixApp;

import static gbench.common.tree.LittleTree.*;
import static gbench.common.tree.LittleTree.IRecord.*;

/**
 * 
 * @author gbench
 *
 */
public class JunitMatrix extends DataMatrixApp{
    
    @Test
    public void foo1() {
        final var aa = DMX(Double.class,Stream.of(A(
                "11/12/13/14","21/22/23/24","31/32/33/34","41/42/43/44"
            )).map(IRecord::STRING2REC).toArray(IRecord[]::new)
        );// aa
        println(aa.tp());
        println(Arrays.asList(aa.getFlatCells()));
        println(Arrays.asList(aa.getFlatCells2()));
        aa.forEachDataCells(cell->{// 修改列表数据
            cell.setValue(100+cell.getValue());
            System.out.println(cell.getAddress()+"-"+cell.offset(1, 1));
        });
        println(aa.tp());
    }//foo
    
    @Test
    public void foo() {
        final var sales = DBLMX(
            R("苹果",100.0,"香蕉",20.0,"葡萄",30.0,"哈密瓜",50.0),
            R("苹果",102.0,"香蕉",204.0,"葡萄",130.0,"哈密瓜",540.0),
            R("苹果",102.0,"香蕉",204.0,"葡萄",130.0,"哈密瓜",540.0),
            R("苹果",102.0,"香蕉",204.0,"葡萄",130.0,"哈密瓜",540.0),
            R("苹果",102.0,"香蕉",204.0,"葡萄",130.0,"哈密瓜",540.0)
        );
        
        final var prices = DBLMX2( 
            "价格1", R("苹果",5.8,"香蕉",3.5,"葡萄",8.5,"哈密瓜",12.4),
            "价格2", R("苹果",4.8,"香蕉",5.6,"葡萄",12.8,"哈密瓜",8.4)
        );
        
        System.out.println(sales);
        System.out.println(prices);
        System.out.println(sales.mmult(prices,"价格1,价格2"));
        System.out.println(sales.div2(1.0));
    }
    
    public static long factorial(long n) {
        return n==0?1:n*factorial(n-1);
    }
    
    public static double power(double x,long n) {
        return n==0?1:x*power(x,n-1);
    }
    
    @Test
    public void foo2() {
       final var elu = new ExpressionEvaluator();
       final var x = 1.0;
       final var proto = REC("elu",elu);
       final var r = NATS(39).map(n->REC("Fn",factorial(n),"Xn",power(x,n)))
       .collect(Collectors.summarizingDouble(e->proto.evalExpr("1/#Fn*#Xn",e))).getSum();
       System.out.println(r);
    }
    
 
    @AllArgsConstructor @ToString @Data @NoArgsConstructor
    public static class User{String name;String sex;}
    @Test
    public void foo3() {
       var rec = REC("name","李四","sex","man");
       System.out.println(rec.toTarget(User.class));
       System.out.println(rec.toTarget(String.class));
       Function<Object,IRecord> p = IRecord::P;
       final var t = Stream.of(p,p,p,p,p,p,p,p,p,p,p).reduce(rec,(e,f)->f.apply(e),(a,b)->a);
       System.out.println(t);
    }
    
    @Test
    public void bar() {
        final var numbers = "1:3";// 数据长度
        final var dd = cph2(series,numbers,numbers,numbers).collect(dmc).setHeader("a,b,c");
        println(dd.filter2("b"));
        final var v1 = dd.filter2("b").mmult(new Double[][] {{1d},{1d}});
        final var v2 = dd.filter2("b").perceptron(0d, new Double[] {1d,1d}, e->e,"y");
        final var x = v1.addColumns(v2);
        println(x);
    }
}
